<?php
if (!defined('ZAZA')) exit('No direct script access allowed');
/**
 * @copyright     ZaZa Group
 * @author        Sir Doan
 */

class Url
{
    /**
     * 
     */
    public static function buildUrl($params = array(), $fnc = false, $res = false)
    {
        $strRequest = ($res ? '?' : '#') .'fnc='. ($fnc ? $fnc : $_REQUEST['fnc']); 
        $strRequest .= Obj()->get('gameid', 'int', 0) != 0 ? '&gameid='. Obj()->get('gameid', 'int', 0) : '';       
        if(sizeof($params) > 0) 
        {
            foreach($params as $key => $param)
                $strRequest .= '&'. $key .'='. $param; 
        }
        return $strRequest;
    }  
    
    /**
     * 
     */
    public function redirect($params = array(), $fnc = false, $res = false)
    {
        if($res)
            header('Location:'. ROOT_URL . DS . Url::buildUrl($params, $fnc));
        else
            echo Obj()->get('callback') .'({"redirect": '. json_encode(Url::buildUrl($params, $fnc)) .'})'; 
        exit(); 
    }
    
    /**
     * Redirect Url
     * 
     * @param String $url
     * @access Public
     */
    public function redirectUrl($url = '#', $res = false)
    {
        if($res)
            header('Location:'. $url); 
        else
            echo Obj()->get('callback') .'({"redirect": '. json_encode($url) .'})'; 
        exit();
    }
    
    /**
     * Error
     * 
     * @param Strong $mes [default: null]
     * @return String
     * @access Public 
     */
    public function error($mes = '', $back = false)
    {
        echo Obj()->get('callback') .'({"error": '. json_encode($mes) . ($back ? ', "back": 1' : '') .'})';
        exit();    
    } 
}
?>
